package com.ws.universal.tools.utils;

import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.ws.universal.tools.wsui.help.TouchableSpan;

import java.util.IllegalFormatException;

/**
 * @创建者 ws
 * @TIME 2021/8/31 16:16
 * @描述
 */
public class StringUtils {

    public static StringBuilder sp = new StringBuilder();

    public static String getString(String... arg) {
        sp.setLength(0);
        for (int i = 0; i < arg.length; i++) {
            sp.append(arg[i]);
        }
        return sp.toString();
    }

    public static String getString(int str) {
        sp.setLength(0);
        sp.append(str);
        return sp.toString();
    }

    public static String getString(Object... str) {
        sp.setLength(0);
        for (int i = 0; i < str.length; i++) {
            sp.append(str[i]);
        }
        return sp.toString();
    }

    public static boolean isEmpty(final CharSequence s) {
        return s == null || s.length() == 0;
    }

    public static boolean isSpace(final String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Return whether string1 is equals to string2.
     *
     * @param s1 The first string.
     * @param s2 The second string.
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean equals(final CharSequence s1, final CharSequence s2) {
        if (s1 == s2) return true;
        int length;
        if (s1 != null && s2 != null && (length = s1.length()) == s2.length()) {
            if (s1 instanceof String && s2 instanceof String) {
                return s1.equals(s2);
            } else {
                for (int i = 0; i < length; i++) {
                    if (s1.charAt(i) != s2.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * Format the string.
     *
     * @param str  The string.
     * @param args The args.
     * @return a formatted string.
     */
    public static String format(@Nullable String str, Object... args) {
        String text = str;
        if (text != null) {
            if (args != null && args.length > 0) {
                try {
                    text = String.format(str, args);
                } catch (IllegalFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        return text;
    }

    public static SpannableString generateSp(TextView tv, String text, PublicDiaologCallback callback) {
        String highlight1 = "Service Agreement";
        String highlight2 = "服务协议";
        String highlight3 = "Privacy Policy";
        String highlight4 = "隐私政策";
        SpannableString sp = new SpannableString(text);
        int start = 0, end;
        int index;
        while ((index = text.indexOf(highlight1, start)) > -1) {
            end = index + highlight1.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {
                    callback.onConfirm();
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }
        start = 0;
        while ((index = text.indexOf(highlight2, start)) > -1) {
            end = index + highlight2.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {
                    callback.onConfirm();
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }

        start = 0;
        while ((index = text.indexOf(highlight3, start)) > -1) {
            end = index + highlight3.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {
                    callback.onCancel();
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }
        start = 0;
        while ((index = text.indexOf(highlight4, start)) > -1) {
            end = index + highlight4.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {
                    callback.onCancel();
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }

        return sp;
    }

    public interface PublicDiaologCallback {
        void onConfirm();

        void onCancel();
    }

    //身份标识
    public static String setMsg(long msg) {
        String name = "Please swipe your bank card<br/> again within  <font color=\"#FF0000\"> " + msg + "</font> seconds";
        return name;


    }
}
